#include "sendsignal_f.h"
#include <math.h>
#include <float.h>
#include <stdio.h>
#include <iostream>

sendsignal_f::sendsignal_f() {
}

sendsignal_f::~sendsignal_f() {
}

void sendsignal_f::send(double* signal, int) {
  const int neuron_in_layer       = layersize;
  const int layers_count          = getlayercountmax();
  int       start_index           = 0;
  int       start_prev_index      = 0;
  int       index                 = 0;
  int       prev_index            = 0;
  int       layer                 = 0;
  int       synapse_count         = 0;
  synapse   syn_val;
  axon      axon_val;
  double    fsignal;

/* устанавливаем сигнал */
  for(index = 0; index < neuron_in_layer; index ++) {
    for(synapse_count = 0; synapse_count < neuron_in_layer; synapse_count ++) {
      syn_val = p_neuron[index].getSynapse(synapse_count);
      syn_val.setFSignal(signal[synapse_count]);
      p_neuron[index].setSynapse(syn_val, synapse_count);
    }
  }


/* вычисляем нейроны нулевого (входного) слоя */
  for(index = 0; index < neuron_in_layer; index ++) {
    (void)p_neuron[index].calc();
  }

/* вычисляем остальные слои */
  for(layer = 1; layer < layers_count; layer ++)
  {
    start_index      = getstartneuronidforlayer(layer);
	start_prev_index = getstartneuronidforlayer(layer-1);
    for(index = 0; index < neuron_in_layer; index ++)
	{
/*обновляем синапсы */
      for(synapse_count = 0; synapse_count < neuron_in_layer; synapse_count++)
	  {
        prev_index = start_prev_index + synapse_count;
        axon_val = p_neuron[prev_index].getAxon();
        fsignal = axon_val.getFSignal();

		syn_val = p_neuron[index + start_index].getSynapse(synapse_count);
		syn_val.setFSignal(fsignal);
		p_neuron[index + start_index].setSynapse(syn_val, synapse_count);
	  }
	}
/* вычисляем нйроны для данного слоя */
    for(index = 0; index < neuron_in_layer; index ++)
      p_neuron[index + start_index].calc();
  }
}

#if 0
  ntype::error_t sendsignal_f::geterror() {
    const int in_size      = layersize;
    const int layers_count = getlayercountmax();
    const int start_index  = getstartneuronidforlayer(layers_count -1);
    double    ret_error    = 0.;
    double    calc_val;
    double    insig;
    double    outsig;


    for(int icount = 0; icount < in_size; icount ++)
    {
      insig = p_neuron[0].getSynapse(icount).getFSignal();
      outsig = p_neuron[start_index + icount].getAxon().getFSignal();
      if(insig <= ZERO_VALUE && outsig <= ZERO_VALUE)
          calc_val = 0.0;
      else
      {
        calc_val = (outsig - insig) / insig;
        calc_val = fabs(calc_val);
      }

//getLog()->print("err: -> %f  : in_sig -> %f : out_sig -> %f\n", ret_error, insig, outsig);
      ret_error += calc_val;
//printf("err:->%f:in_sig->%f:out_sig->%f:calc_value->%f\n", ret_error, insig, outsig, calc_val);
std::cout << "err:  " << ret_error << " in_sig: "<< insig << " out_sig: " <<  outsig << std::endl;
  }
  ret_error = ret_error / in_size;
//printf("result error: %f, size: %d\n", ret_error, in_size);
  return ret_error < MIN_ERROR_VALUE ? 0.0 : ret_error;
}
#endif

